<?php
	/**
	 * 记录
	 * Created by Notepad++
	 * User: Jovan
	 * Date: 2017/9/1
	 * Time: PM 15:00
	 */
	header("Content-type: text/html; charset=utf-8");

	// 实例 (MySQLi - 面向对象)

	$servername = "localhost";
	$username   = "root";
	$password   = "root";
	$dbname     = "sql_name";
	 
	// 创建连接
	$conn = new mysqli($servername, $username, $password, $dbname);
	// Check connection
	if ($conn->connect_error) {
		die("连接失败: " . $conn->connect_error);
	}
	
	// 添加留言
	if( !empty($_POST['content']) ){
		if( empty($_POST['content']) ){
			$nicheng = iconv("utf-8", "gbk", "佚名");
		}else{
			$nicheng = iconv("utf-8", "gbk", $_POST['nicheng']);
		}
		$biaoti  = iconv("utf-8", "gbk", $_POST['biaoti']);
		$content = iconv("utf-8", "gbk", $_POST['content']);
		$timer   = time();   // 获得时间戳
		$getip   = Getip();  // 获得访客真实ip
		$sql = "INSERT INTO record (nicheng, biaoti, content, login_last_time, login_last_ip) VALUE ('".$nicheng."', '".$biaoti."', '".$content."',". $timer .", '".$getip."')";
		$result = $conn->query($sql);
		if($result){
			echo "<script>alert('提交成功！');location.href=\"/xiaoli/index.php\";</script>";
		}else{
			echo "<script>alert('提交失败！');location.href=\"/xiaoli/index.php\";</script>";
		}
	}
	
	
	////获得访客真实ip
	function Getip(){
		if(!empty($_SERVER["HTTP_CLIENT_IP"])){   
			$ip = $_SERVER["HTTP_CLIENT_IP"];
		}
		if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //获取代理ip
			$ips = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
		}
		if($ip){
			$ips = array_unshift($ips,$ip); 
		}

		$count = count($ips);
		for($i=0;$i<$count;$i++){   
			if(!preg_match("/^(10|172\.16|192\.168)\./i",$ips[$i])){//排除局域网ip
				$ip = $ips[$i];
				break;    
			}  
		}  
		$tip = $ip ? $ip : $_SERVER['REMOTE_ADDR']; 
		if($tip=="127.0.0.1"){ //获得本地真实IP
			return $this->get_onlineip();   
		}else{
			return $tip; 
		}
	}
	
	 ////获得本地真实IP
	function get_onlineip(){
		$mip = file_get_contents("http://www.ip138.com/ip2city.asp");
		if($mip){
			preg_match("/\[.*\]/",$mip,$sip);
			$p = array("/\[/","/\]/");
			return preg_replace($p,"",$sip[0]);
		}else{return "获取本地IP失败！";}
	}

?>

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title></title>
</head>
<style>
	*,body{margin: 0;padding: 0;list-style: none;}
	.one{margin: 0 auto;width: 60%;padding: 1%;}
	span{width: 100%;display: inline-block;line-height:24px;margin: 20px 0px 5px;}
	span,input,textarea{font-family: monospace;font-size: x-large;}
	input{width: 25%;text-indent: 5px;}
	input,textarea{outline:none;padding: 3px;font-size: 16px;}
	textarea{width: 95%;resize: none;/*彻底禁止拖动*/height:150px;max-height:200px;padding: 5px;}
	.zidong{text-align: right;font-size: 12px;color: #ccc;margin: 0px 0% 0px;width: 95%;}
    .tijiao{width: 8%;padding: 8px;color: #292828;font-weight: bold;cursor: pointer;border-radius: 3px;margin: 20px 0px 10px 44%;}
	.son{border: 1px solid #f1f1f1;width: 95%;padding: 5px;margin:3px 0px;}
	.p_one,p{font-family: monospace;font-size: x-large;}
	.p_two{border-top: 1px solid #f6f6f6;border-bottom: 1px solid #f9f9f9;padding: 5px 0px;word-break: break-all;/*这里可以防止溢出*/}
	.p_three{text-align: right;}
	.p_one,.p_three{font-weight: bold;}
	.p_four{text-align:center;text-indent:2%;}
	.p_son{margin: 0px 0px 0px 1%;width: 50%;}
	
	#backTop{display: none;text-decoration: none;padding:10px;border:1px solid #fff;background-color: #d7ffaa;position: fixed;width: 32px;bottom: 30px;right:30px;color:#333;}
	#systime{position:fixed;bottom: 0;left: 0.5%;}
</style>
<body>
	<div class="one">
		<form action="/xiaoli/index.php" method="post">
			<span>昵 称:</span>
			<input type="text" name="nicheng" placeholder="๑乛◡乛๑ 磨人的小妖精">
			<span>标 题:</span>
			<input type="text" name="biaoti" placeholder="✿✿ヽ(ﾟ▽ﾟ)ノ✿完结撒花！">
			<span>内心独白:</span>
			<textarea type="text" name="content" placeholder="请输入...           ╭( ′• o •′ )╭☞警察叔叔！就是这个人！"></textarea>
			<span class="zidong">留言三天后自动显示</span>
			<input class="tijiao" type="submit" value="确认提交">
		</form>
	</div>
	<div class="one">
		<?php // 输出留言
			$sql = "SELECT id, biaoti, nicheng, content, login_last_time FROM record";
			$result = $conn->query($sql);
			if ($result->num_rows > 0){
				// 输出数据
				$t = time();
				while($row = $result->fetch_assoc()){
					if($row['login_last_time'] + 259200 < $t){  // 大于三天时间就显示出来
					// if($row['login_last_time'] < $t){  // 大于三天时间就显示出来
		?>
		<div class="son">
			<p class="p_one"><?=iconv("gbk", "utf-8", $row['nicheng']); ?>
				:<span class="p_son"><?=iconv("gbk", "utf-8", $row['biaoti']); ?></span>
			</p>
			<p class="p_two"><?=iconv("gbk", "utf-8", $row['content']); ?></p>
			<p class="p_three"><?=date("Y-m-d H:i", $row['login_last_time']); ?></p>
		</div>
		<?php 
					}
				}
			} else{
		?>
		<div class="son">
			<p class="p_four"><?php echo "没有留言内容！ ╮(╯﹏╰）╭  你还不快来留言。"; ?></p>
		</div>
		<?php }
			$conn->close();
		?>
	</div>
	
	<a href="javascript:void(0)" id="backTop">返回顶部</a>
	<script type="text/javascript" src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>
	<script type="text/javascript">
		window.onload = function(){
			var backTop = document.getElementById("backTop");
			var target = 0, leader = 0, timer = null;
			
			document.onscroll = function(){
				var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
				leader = scrollTop;
				if (scrollTop > 0) {
					backTop.style.display = "block";
				}else{
					backTop.style.display = "none";
				}
			}
			backTop.onclick = function(){
					var timer = setInterval(function(){
						leader = leader + (target - leader) / 10;
						window.scrollTo(0, leader);
						if (target == leader) {
							clearInterval(timer);
							console.log("ok");
						};	
					},10);		
			}
			
		}
	</script>
	
	<script type="text/javascript">
		Date.prototype.Format = function(fmt) { //author: meizz   
			var o = {
				"M+" : this.getMonth() + 1, //月份   
				"d+" : this.getDate(), //日   
				"h+" : this.getHours(), //小时   
				"m+" : this.getMinutes(), //分   
				"s+" : this.getSeconds(), //秒   
				"q+" : Math.floor((this.getMonth() + 3) / 3), //季度   
				"S" : this.getMilliseconds()
			//毫秒   
			};
			if (/(y+)/.test(fmt))
				fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
						.substr(4 - RegExp.$1.length));
			for ( var k in o)
				if (new RegExp("(" + k + ")").test(fmt))
					fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
							: (("00" + o[k]).substr(("" + o[k]).length)));
			return fmt;
		}
		setInterval(
				"systime.innerHTML=new Date().Format('yyyy-MM-dd hh:mm:ss')+' 星期'+'日一二三四五六'.charAt(new Date().getDay());",
				1000);
	</script>
	<p id="systime"></p>

</body>
</html>
